var from;
layui.use(['element', 'form', 'layer', 'table'], function () {
    form = layui.form;
    var element = layui.element
        , table = layui.table
        , layer = layui.layer;
    table.render({
        elem: '#userlist'
        , id: 'uReload'
        , height: 'full-220'
        , method: 'post'
        , url: '/Index/getColor' //数据接口
        , cols: [[ //表头
            { field: 'ids', title: '#', width: 50, type: 'numbers', fixed: 'left' }
            , { field: 'name', title: '颜色名称', width: 100 }
            , { field: 'status', title: '选择状态', width: 110, templet: "<div>{{ status(d.status,d.id)}}</div>" }
        ]]
    });

    //监听颜色选择
    form.on('checkbox(lockstatus)', function (obj) {
        var e = layer.load(2, { shade: [0.2, '#2F4056'] });
        if (obj.elem.checked == false) {
            value = 0;
        } else {
            value = 1;
        }
        $.post("/Index/updateColor", { id: this.value, field: 'status', value: value }, function (msg) {
            layer.close(e);
            layer.msg(msg, { time: 1000 });
            window.parent.location.reload();
        });
    });
});

//传入URL前缀及字段值
function status(vals, id) {
    //判断是否需要拼接URL
    if (vals === 0) {
        result = '<input type="checkbox" name="lock" value="' + id + '" title="启用" lay-filter="lockstatus">';
    } else if (vals === 1) {
        result = '<input type="checkbox" name="lock" value="' + id + '" title="启用" lay-filter="lockstatus" checked="checked">';
    } else {
        result = vals;
    }
    return result;
}

